/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package automatolfa;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author IVAN
 */
public class ReadXMLFile {

    private List<String> _lstTokens;

    public ReadXMLFile() {
        _lstTokens = new ArrayList<>();
    }

    public ReadXMLFile(List<String> lstToken) {
        _lstTokens = lstToken;
    }
    
    public List<String> RetornaTokens()
    {
        try {            
            
            String tokenXmlDir = new File(".").getAbsolutePath();
            File fXmlFile = new File(tokenXmlDir.substring(0, (tokenXmlDir.length()-1)) +  "src\\lna\\resource\\token.xml" );
            
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            
            NodeList nList = doc.getElementsByTagName("token");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    getLstTokens().add(getTagValue("nome", eElement));

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return _lstTokens;
    }

    private String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }

    /**
     * @return the _lstTokens
     */
    public List<String> getLstTokens() {
        return _lstTokens;
    }

    /**
     * @param aLstTokens the _lstTokens to set
     */
    public void setLstTokens(List<String> aLstTokens) {
        _lstTokens = aLstTokens;
    }
}
